package com.songcan.oauth.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.songcan.common.annotation.JSON;
import com.songcan.common.context.UserContext;
import com.songcan.common.exception.ParamNotException;
import com.songcan.common.utils.TreeUtil;
import com.songcan.common.vo.DeptVO;
import com.songcan.common.vo.MenuVo;
import com.songcan.model.oauth.SysDept;
import com.songcan.model.oauth.SysMenu;
import com.songcan.model.oauth.SysPosition;
import com.songcan.oauth.service.SysDeptService;
import com.songcan.oauth.service.SysMenuService;
import com.songcan.oauth.service.SysPositionService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@AllArgsConstructor
@RestController
@RequestMapping("rest/common")
public class CoreController {

    private final SysDeptService sysDeptService;
    private final SysMenuService sysMenuService;
    private final SysPositionService sysPositionService;


    @GetMapping("/dept")
    public List<DeptVO> treeDept(){
        List<SysDept> depts = sysDeptService.list(Wrappers.lambdaQuery(new SysDept()).eq(SysDept::getDelFlag,0));

        if(Objects.nonNull(depts)&&depts.size()>0) {
            List<DeptVO> list = depts.parallelStream()
                    .map(p -> {
                        DeptVO deptVO = new DeptVO();
                        deptVO.setId(p.getDeptId());
                        deptVO.setName(p.getName());
                        deptVO.setParentId(p.getParentId());
                        return deptVO;
                    }).collect(Collectors.toList());
          return TreeUtil.buildByRecursive(list,list.get(0).getId());
        }
        return  new ArrayList<>();
    }


    @GetMapping("/menu")
    @JSON(type = MenuVo.class,filter = "url,scope,role,parentId,code,id")
    public List<MenuVo> treeMenu( Long id){
        Integer type = 0;
        List<String> roles =UserContext.getRoles();
        if(Objects.isNull(roles)||roles.size()<1){
            throw new ParamNotException("不合法用户");
        }
        if(Objects.nonNull(id)){
            type = 1;
        }
        List<SysMenu> list = sysMenuService.selectMenuByUserId(roles,id,type);
        if(Objects.isNull(list)&&list.size()<1){
            throw new ParamNotException("对应角色角色不存在");
        }

        List<MenuVo> menuVos = TreeUtil.buildByLoop(list.parallelStream().map(p->{
            MenuVo menuVo = new MenuVo();
            menuVo.setId(p.getMenuId());
            menuVo.setParentId(p.getParentId());
            menuVo.setPath(p.getPath());
            menuVo.setRedirect(p.getRedirect());
            menuVo.setComponent(p.getComponent());
            menuVo.setHidden(p.getStatus()==1);
            menuVo.setName(p.getName());
            MenuVo.Meta meta = new MenuVo.Meta();
            meta.setIcon(p.getIcon());
            meta.setTitle(p.getTitle());
            menuVo.setMeta(meta);
            return menuVo;
        }).collect(Collectors.toList()),list.get(0).getParentId());

        return TreeUtil.buildByLoop(menuVos,list.get(0).getParentId());
    }

    @GetMapping("/position")
    @JSON(type = SysPosition.class,filter = "description,createTime")
    public List<SysPosition> positionList(String name){
        LambdaQueryWrapper<SysPosition> sysPositionLambdaQueryWrapper = Wrappers.lambdaQuery(new SysPosition());
        if(Objects.nonNull(name)&&StrUtil.isNotEmpty(name)){
            sysPositionLambdaQueryWrapper.like(SysPosition::getName,"%"+name+"%");
        }
        sysPositionLambdaQueryWrapper.orderByDesc(SysPosition::getCreateTime);
        return  sysPositionService.list(sysPositionLambdaQueryWrapper);
    }
}
